//
//  GLGradualView.swift
//  uisliderDel
//
//  Created by quan on 2019/8/14.
//  Copyright © 2019 quan. All rights reserved.
//

import UIKit

enum GLGradualViewType {
    case fromBottomToTop
    case fromTopToBottom
}


class GLGradualView: UIView {
    var type: GLGradualViewType
    var gradientLayer = CAGradientLayer.init()
    
    init(type: GLGradualViewType) {
        self.type = type
        super.init(frame: .zero)
        
//        gradientLayer.frame = CGRect(x: 0, y: UIScreen.main.bounds.height - height, width: UIScreen.main.bounds.width, height: height)
        gradientLayer.colors = [getFromColor(), getToColor()]
        gradientLayer.startPoint = CGPoint(x: 0.5, y: 1.0)
        gradientLayer.endPoint = CGPoint(x: 0.5, y: 0.0)
        gradientLayer.locations = [NSNumber(value: 0.0)]
        self.layer.addSublayer(gradientLayer)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        gradientLayer.removeFromSuperlayer()
        gradientLayer.frame = self.bounds
        self.layer.insertSublayer(gradientLayer, at: 0)
    }
    
    
    private func getFromColor() -> CGColor {
        if type == .fromBottomToTop {
            return UIColor.init(red: 22/255.0, green: 23/255.0, blue: 24/255.0, alpha: 1.0).cgColor
        }
        else {
            return UIColor.init(red: 22/255.0, green: 23/255.0, blue: 24/255.0, alpha: 0.0).cgColor
        }
    }
    
    private func getToColor() -> CGColor {
        if type == .fromBottomToTop {
            return UIColor.init(red: 22/255.0, green: 23/255.0, blue: 24/255.0, alpha: 0.0).cgColor
        }
        else {
            return UIColor.init(red: 22/255.0, green: 23/255.0, blue: 24/255.0, alpha: 1.0).cgColor
        }
    }

}
